<?php
/*
 * @Description  : 壁纸图片管理
 * @Author       : https://github.com/skyselang
 * @Date         : 2020-03-30
 * @LastEditTime : 2020-12-25
 */

namespace app\admin\controller;

use app\admin\service\ImgService;
use app\admin\validate\ImgValidate;
use think\facade\Config;
use think\facade\Filesystem;
use think\facade\Request;
use think\Image;

class Img
{
    /**
     * 图片壁纸列表
     *
     * @method GET
     * 
     * @return json
     */
    public function imgList()
    {
        $page  = Request::post('page/d', 1);
        $limit = Request::post('limit/d', 10);
        $getAll = Request::post('get_all/b', false);
        $field = Request::post('field/s', "*"); //寻找字段
        $param = array( 
            'category_ids'      => Request::post("category_ids/a",[]),
            'name'              => Request::post("name/s",''),
            'title'             => Request::post("title/s",''),
            'source_name'       => Request::post("source_name/s",''),
            'status'            => Request::post("status/d",''),
            'create_time_start' => Request::post("create_time_start/s",''),
            'create_time_end'   => Request::post("create_time_end/s",'')
        );
        $data = ImgService::findList($param, $page, $limit, $field,$getAll);
        return success($data);
    }


    /**
     * 图片壁纸添加
     *
     * @method POST
     * 
     * @return json
     */
    public function imgAdd()
    {
       
        $param = array(
            'category_ids' => Request::post("category_ids/a",[]),
            'name'         => Request::post("name/s",''),
            'title'        => Request::post("title/s",''),
            'sort'         => Request::post("sort/d",1),
            'source_name'  => Request::post("source_name/s",'本地上传'),
            'source_path'  => Request::post("source_path/s",'localhost'),
            'status'       => Request::post("status/d",1),
            'remark'       => Request::post("remark/s",''),
            'set_path'     => Request::post('set_path/s')
        );
        validate(ImgValidate::class)->scene('img_add')->check($param);
        //数据添加
        return ImgService::add($param);
    }

    /**
     * 图片壁纸修改
     *
     * @method POST
     * 
     * @return json
     */
    public function imgEdit()
    {
        $categoryIdList = Request::post("category_ids",'');
        $param = array(
            'img_id'       => Request::post("img_id/d",0),
            'category_ids' => is_array($categoryIdList) ? implode(",",$categoryIdList) : $categoryIdList,
            'name'         => Request::post("name/s",''),
            'title'        => Request::post("title/s",''),
            'sort'         => Request::post("sort/d",1),
            'source_name'  => Request::post("source_name/s",'本地上传'),
            'source_path'  => Request::post("source_path/s",'localhost'),
            'remark'       => Request::post("remark/s",''),
            'y_imagePath'  => Request::post("y_imagePath/s",''),
            'file'         => Request::file('images')
        );
        validate(ImgValidate::class)->scene('img_edit')->check($param);
        //文件上传处理
        if(!Request::file('images') && Request::file('images') != null){
            $savename = Filesystem::disk("bizhi")->putFile('bz',Request::file('images'));
            if(!$savename){
                return error('文件上传失败！');
            }
            unset($param['file']);
            $param['set_path'] = str_replace("\\","/",$savename);
            //生成缩略图
            $path = Config::get("filesystem.disks.bizhi.root");
            $filePath = str_replace("\\","/",$path."/".$param['set_path']);
            $fileName = basename($filePath);
            $s_path = str_replace($fileName,"s_".$fileName,$filePath);
            try{
                if(is_file($filePath)){
                    $image = Image::open($filePath);
                    $image->thumb(160, 266)->save($s_path);
                }
            }catch(\Exception $e){
                if(is_file($filePath)){
                    unlink($filePath);
                }
                if(is_file($s_path)){
                    unlink($s_path);
                }
                return error($e->getMessage());
            }
        }
        //数据修改
        return ImgService::edit($param);
    }
}
